﻿$(function () {

    init();

});

function init(queryData) {
    $('#list').datagrid({
        title: '公司管理',
        idField: 'CompanyID',
        fitColumns: 'true',
        autoRowHeight: true,
        nowrap: true,
        striped: true,
        pagination: true,
        url: '/Company/GetList',
        columns: [[
                    { field: "CompanyID", title: "ID", hidden: true },
                    { field: 'ck', checkbox: true },
                    { field: 'CompanyName', title: '单位名称', width: 180 },
                    { field: 'Address', title: '地址', width: 180 },
                    { field: 'Telephone', title: '电话', width: 180 },
                    { field: 'EMAIL', title: '电子邮箱', width: 180 }
        ]],
    });
}

function add() {
    $('#dlg').dialog('open').dialog('setTitle', '增加');
    $('#fm').form('clear');
    url = '/Company/Create';
}

function edit() {
    var updateids = $("#list").datagrid("getSelections");
    if (updateids.length == 1) {
        var updateID = $('#list').datagrid('getSelections')[0].CompanyID;
        $.getJSON("/Company/GetEntity", { ID: updateID }, function (date) {
            //绑定数据显示到用户控件上面
            $('#fm').form('load', date);
        })
        $('#dlg').dialog('open').dialog('setTitle', '修改');
        url = '/Company/Edit';
    }
    else {
        $.messager.alert("友情提示", "每次只能修改一行数据，你已经选择了<font color='red' size='6'>" + UpdateDictionaryInfoID.length + "</font>行");
    }
}

function save() {
    $('#fm').form('submit', {
        url: url,
        onSubmit: function () {
            return $(this).form('validate');
        },
        success: function (result) {
            if (result == 'OK') {
                $('#dlg').dialog('close');      // close the dialog  
                $('#list').datagrid('reload');    // reload the user data  
            } else {
                $.messager.show({
                    title: 'Error',
                    msg: result
                });
            }
        }
    });
}

function del() {
    var rows = $('#list').datagrid('getSelected');
    if (rows.length >= 1) {
        $.messager.confirm('Confirm', 'Are you sure you want to remove this item?', function (r) {
            if (r) {
                $.post('/Company/Delete', { id: rows.CompanyID },
                    function (result) {
                        if (result == 'OK') {
                            $("#list").datagrid('clearSelections');
                            $('#list').datagrid('reload');    // reload the user data  
                        } else {
                            $.messager.show({   // show error message  
                                title: 'Error',
                                msg: result
                            });
                        }
                    });
            }
        });
    }
}
